package com.spzx.cart.controller;

import com.spzx.api.cart.domain.CartInfo;
import com.spzx.cart.service.CartService;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.annotation.RequiresLogin;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.spzx.common.core.web.domain.AjaxResult.success;

@Tag(name = "购物车接口")
@RestController
@RequestMapping
public class CartController {
    @Autowired
    private CartService cartService;


    //添加到购物车
    @Operation(summary = "添加购物车")
    @RequiresLogin  //用户只有登录了之后才能访问该方法
    @GetMapping("addToCart/{skuId}/{skuNum}")
    //@Parameter在此的作用是描述参数信息
    public AjaxResult addToCart(@Parameter(name = "skuId", description = "商品skuId", required = true) @PathVariable("skuId") Long skuId,
                                 @Parameter(name = "skuNum", description = "数量", required = true) @PathVariable("skuNum") Integer skuNum){
        cartService.addToCart(skuId,skuNum);
        return success();
    }
    //查询购物车
    @Operation(summary = "查询购物车")
    @RequiresLogin
    @GetMapping("cartList")
    public AjaxResult cartList() {
        List<CartInfo> cartList = cartService.getCartList();
        return success(cartList);
    }

    //删除购物车中商品
    @Operation(summary = "删除购物车")
    @RequiresLogin
    @DeleteMapping("deleteCart/{skuId}")
    public AjaxResult deleteCart(@Parameter(name = "skuId", description = "商品skuId", required = true) @PathVariable("skuId") Long stuId){
        cartService.deleteCart(stuId);
        return success();
    }

    @Operation(summary = "更新购物车商品选中状态")
    @RequiresLogin
    @GetMapping("checkCart/{skuId}/{isChecked}")
    public AjaxResult deleteCart(@Parameter(name = "skuId", description = "商品skuId", required = true) @PathVariable("skuId") Long skuId,
                                 @Parameter(name = "isChecked", description = "是否选中 1:选中 0:取消选中", required = true) @PathVariable("isChecked") Integer isChecked){
        cartService.updateCart(skuId,isChecked);
        return success();
    }

    @Operation(summary="更新购物车商品全部选中状态")
    @RequiresLogin
    @GetMapping("allCheckCart/{isChecked}")
    public AjaxResult allCheckCart(@Parameter(name = "isChecked", description = "是否选中 1:选中 0:取消选中", required = true) @PathVariable(value = "isChecked") Integer isChecked){
        cartService.allCheckCart(isChecked);
        return success();
    }
    @Operation(summary="清空购物车")
    @RequiresLogin
    @GetMapping("clearCart")
    public AjaxResult clearCart(){
        cartService.clearCart();
        return success();
    }

    @Operation(summary="查询用户购物车列表中选中商品列表")
    @RequiresLogin
    @GetMapping("getCartCheckedList")
    public R<List<CartInfo>> getCartsWithChecked(){
        List<CartInfo> cartInfoList = cartService.getCartCheckedList();
        return R.ok(cartInfoList);
    }


    @Operation(summary="更新用户购物车列表价格")
    @RequiresLogin
    @GetMapping("updateCartPrice/{userId}")
    public R<Boolean> updateCartPrice(@PathVariable Long userId){
        Boolean b = cartService.updateCartPrice(userId);
        return R.ok(b);
    }


}
